package com.sky.service.user.impl;

import com.sky.context.BaseContext;
import com.sky.mapper.user.AddressBookMapper;
import com.sky.pojo.AddressBook;
import com.sky.service.user.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookMapper addressBookMapper;
    @Override
    public void save(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        addressBookMapper.save(addressBook);
    }

    @Override
    public List<AddressBook> select() {
        return addressBookMapper.select(BaseContext.getCurrentId());
    }

    @Override
    public AddressBook selectById(Long id) {
       return addressBookMapper.selectById(id);
    }

    @Override
    public void update(AddressBook addressBook) {
        addressBookMapper.update(addressBook);
    }

    @Override
    public void delete(Long id) {
        addressBookMapper.delete(id);
    }

    @Override
    public void setDefault(Long id) {
       AddressBook addressBook = addressBookMapper.selectById(id);
       log.info("查询的返回的地址为:{}",addressBook);
        if (addressBook.getIsDefault()==1){
            addressBookMapper.setDefault2(id);
        }else {
            addressBookMapper.setNotDefault(BaseContext.getCurrentId());
            addressBookMapper.setDefault(id);
        }
    }
}
